package com.wskj.library.dao.impl;

import com.wskj.library.pojo.*;

import com.wskj.library.dao.*;

import java.util.List;

/**
 * bookInfo dao接口类实现类
 * 2018-03-22 T148
 */ 
public class BookInfoDaoImpl extends BaseDao implements BookInfoDao{

	 @Override
	 public int addBookInfo(BookInfo  b){
		 String sql="insert into bookinfo (bname, author, publishTime, publisher, price, collectDay, cid, state, bimg, sumary) values "
		 		+ "      (?, ?, ?, ?, ?, ?, ?,1,?, ?); ";
		 Object[] params= {b.getBname(),b.getAuthor(),b.getPublishTime(),b.getPublisher(),b.getPrice(),b.getCollectDay(),b.getCid(),b.getBimg(),b.getSumary()};
		 return super.executeUpdate(sql, params);
	 }
	 
	 @Override
	 public int deleteById(int id){
		 String sql="delete from bookInfo  where bid=? ";
		 Object[] params= {id};
		 return super.executeUpdate(sql, params);
	 }
	 @Override
	 public int updateBookInfo(BookInfo bookInfo){
		 String sql="update bookInfo set  ";
		 Object[] params= {};
		 return super.executeUpdate(sql, params);
	 }
	 @Override
	 public BookInfo getBookInfoById(int id){
		 String sql="select * from bookInfo where bid=? ";
		 Object[] params= {id};
		 return super.queryOne(sql, params,BookInfo.class);
	 }
	 
	 @Override
	 public  List<BookInfo>  getAllBookInfos(){
		 String sql="select b.*,cname from bookInfo b "
		 		+ " inner join bookCategory bc "
		 		+ " on bc.cid=b.cid ";
		 Object[] params= null;
		 return super.queryAll(sql, params, BookInfo.class);
	 }

	@Override
	public List<BookInfo> getBookWithPage(String bname, String author, int cid, int status, int index, int pageSize) {
		String sql="select b.*,cname from bookInfo b"
		 		+ " inner join bookcategory bc on b.cid=bc.cid  "
		 		+ " where bname like concat('%',?,'%') "
		 		+ " and author like concat('%',?,'%') "
		 		+ " and (b.cid=? or -1=?) "
		 		+ " and (state=? or -1=?) "
		 		+ " limit ?,?";
		 Object[] params= {bname,author,cid,cid,status,status,(index-1)*pageSize,pageSize};
		return super.queryAll(sql, params, BookInfo.class);
	}

	@Override
	public long getCoutsByBname(String bname, String author, int cid, int status) {
		 String sql="select count(*)  from bookInfo "		 		
		 		+ " where bname like concat('%',?,'%') "
		 		+ " and author like concat('%',?,'%') "
		 		+ " and (cid=? or -1=?) "
		 		+ " and (state=? or -1=?) ";		 		
		 Object[] params= {bname,author,cid,cid,status,status};
		return super.getCounts(sql, params);
	}

	
	@Override
	public int deleteAllBooks(String[] id) {		
		String sql="delete from bookinfo where bid in (";
		for(int i=0;i<id.length;i++) {
			if(i==id.length-1) {
			sql +="?)";
			}else {
			sql +="?,";
			}
		}		
		Object[] params=id;
		return super.executeUpdate(sql, params);
	}

	
	@Override
	public int saveBooks(List<BookInfo> books) {
		String  sql="insert into bookinfo (bname,author,publishTime,publisher,price,collectDay,cid,sumary) values (?,?,?,?,?,?,?,?); ";
		int count=0;
		for(BookInfo b:books) {
			Object[] params= {b.getBname(),b.getAuthor(),b.getPublishTime(),b.getPublisher(),b.getPrice(),b.getCollectDay(),b.getCid(),b.getSumary()};
		    count +=super.executeUpdate(sql, params);			
		}			
		return count;
	}
	
	
}

